/*
 * This file is part of the SDWebImage package.
 * (c) Olivier Poitrey <rs@dailymotion.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

#import "UIImageView+WebCache.h"
#import "SDImageCache.h"
#import "RoundRectImage.h"
#import "UIButton+WebCache.h"
#import "AppCommunicationManager.h"
#import "AppDataManager.h"

@implementation UIImageView (WebCache)
int imageType = 0;
//添加：yinlinlin,是否裁剪的标志位
bool _cut = NO;
//////downloadingSchedule part

- (void)setShowDownloadingSchedule:(BOOL)show;
{
   if(show)
   {
       if([self downloadingScheduleShow])
           return;
       UILabel *scheduleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20.0f)];
       scheduleView.backgroundColor = [UIColor clearColor];
       scheduleView.tag = ScheduleViewTag;
       scheduleView.font = [UIFont boldSystemFontOfSize:15.0f];
       scheduleView.textColor = [UIColor orangeColor];
       scheduleView.textAlignment = kTextAlignmentCenter;
       scheduleView.hidden = YES;
       [scheduleView setCenter:self.center];
       [self addSubview:scheduleView];
       [scheduleView release];
   }
    else 
    {
        [[self scheduleView] removeFromSuperview];
    }
}

- (id)scheduleView
{
    return [self viewWithTag:ScheduleViewTag];
}

- (BOOL)downloadingScheduleShow
{
    return [self scheduleView]?YES:NO;
}

////////////////////////////

//表情商店：先查询本地是否已下载。已下载时直接返回已下载的图片
-(void)setImageWithUrlString:(NSString *)imageUrl withFolderName:(NSString *)folderName withImageName:(NSString *)imageName
{
    NSString * imageUrlStr = [AppComManager pathForEmo:imageName withFolder:folderName];
    
    if ([FileManager fileExistsAtPath:imageUrlStr])
    {
        //本地路径
        self.image = [UIImage imageWithContentsOfFile:imageUrlStr];
    }
    else
    {
        //网络路径
        [AppComManager getEmoImage:imageUrl withImageName:imageName withFolder:folderName delegate:MyAppDataManager withImageView:self];
    }
}

//魔法文字素材图片
-(void)setImageWithUrlString:(NSString *)imageUrl withFolderName:(NSString *)folderName
{
    NSString * imageUrlStr = [AppComManager pathForMagicWordsImage:imageUrl];
    
    if ([FileManager fileExistsAtPath:imageUrlStr])
    {
        //本地路径
        self.image = [UIImage imageWithContentsOfFile:imageUrlStr];
    }
    else
    {
        //网络路径
        [AppComManager getMagicWordsImage:imageUrl withFolder:MagicWordsFolder delegate:self withImageView:self];
//        [AppComManager getEmoImage:imageUrl  withFolder:folderName delegate:MyAppDataManager withImageView:self];
    }
}

//收藏的表情：先查询本地是否已下载。已下载时直接返回已下载的图片
/*
 imageUrl:网络路径
 ImagePath：本地路径
 folderName:文件夹名
*/

-(void)setImageWithUrlString:(NSString *)imageUrl withFolderName:(NSString *)folderName withImagePath:(NSString *)imagePath
{
    if (imageUrl == nil || folderName == nil || imagePath == nil)
    {
        self.image = nil;
        return;
    }
    NSString * imageUrlStr = [AppComManager pathForEmo:imagePath withFolder:folderName];
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:imageUrlStr])
    {
        //本地路径
        self.image = [UIImage imageWithContentsOfFile:imageUrlStr];
    }
    else
    {
        //网络路径
        [self setImageWithURL:[NSURL URLWithString:imageUrl]];
    }
}


- (void)setImageWithURL:(NSURL *)url
{
    _cut = NO;
    [self setImageWithURL:url placeholderImage:nil andType:0];
}

- (void)setImageWithURL:(NSURL *)url andCut:(BOOL)cut
{
    _cut = YES;
    [self setImageWithURL:url placeholderImage:nil options:0];
    
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder andType:(int)type
{
    _cut = NO;
    [self setImageWithURL:url placeholderImage:placeholder options:0];
    imageType = type;
//    NSLog(@"+++++++++++%d*******%d",type,imageType);
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    // Remove in progress downloader from queue
    [manager cancelForDelegate:self];

    self.image = placeholder;

    if (url)
    {
        if ([[self scheduleView] isKindOfClass:[UILabel class]])
        {
            UILabel *label = (UILabel *)[self scheduleView];
            label.hidden = NO;
            label.frame = CGRectMake(0, (self.bounds.size.height-20)/2, self.bounds.size.width, 20.0f);

        }
        //label.text = [NSString stringWithFormat:@"0%%"];
        [manager downloadWithURL:url delegate:self options:options];
    }
}

#if NS_BLOCKS_AVAILABLE
- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
{
    [self setImageWithURL:url placeholderImage:nil success:success failure:failure];
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
{
//    [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure];
    
    
      [self setImageWithURL:url placeholderImage:placeholder options:0 andType:0 success:success failure:failure ];
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options andType:(NSInteger)type success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
{
    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];

    // Remove in progress downloader from queue
    [manager cancelForDelegate:self];
    imageType = type;
    self.image = placeholder;

    if (url)
    {
        [manager downloadWithURL:url delegate:self options:options success:success failure:failure];
    }
}
#endif

- (void)cancelCurrentImageLoad
{
    [[SDWebImageManager sharedManager] cancelForDelegate:self];
}


-(void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image forURL:(NSURL *)url
{
//    self.image = image;
//
    //添加：yinlinlin:判断是否需要裁剪图片
    UIImage * aImage = image;
    if (_cut == YES)
    {
        
        CGRect rect = CGRectMake((image.size.width - image.size.height) / 2.0, 0, image.size.height, image.size.height);
        
        aImage=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage],rect)];
    }
    self.image = aImage;
    if ([[self scheduleView] isKindOfClass:[UILabel class]])
    {
        UILabel *label = (UILabel *)[self scheduleView];
        label.hidden = YES;
        label.text = nil;
    }
    
    
    if([[NSData dataWithContentsOfFile:[[SDImageCache sharedImageCache] cachePathForKey:[NSString stringWithFormat:@"%@",url]]]length]<4)
    {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        animation.fromValue = [NSNumber numberWithFloat:0.0f];
        animation.toValue = [NSNumber numberWithFloat:1.0f];
        animation.duration = 0.5f;
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        [self.layer addAnimation: animation forKey: @"FadeIn"];
    }
}

@end
